#include "VCatagoryEffectRow.h"
#include "VCatagoryEffectItem.h"
#include "VMain.h"

VCatagoryEffectRow::VCatagoryEffectRow(void) :
    _expanding(false),
    _time(0),
    _starttime(0)
{
}

VCatagoryEffectRow::~VCatagoryEffectRow(void)
{
}

void VCatagoryEffectRow::init()
{
    int i = 0;
    VCatagoryEffectItem* item = NULL;

    for (; i != VCatagoryEffectRow::item_count; ++i)
    {
        item = new VCatagoryEffectItem;
        item->setGeometry(_x, _y, item_w, item_h);
        item->loadTexture(
            VMain::getSkin("catagory_ui\\catagory%d.png",
                           item_count * *((int*)(privateData())) + i + 1
                          ));
        item->move(_x + (item_x_margin + item_w) * i * 0, _y);
        item->setPrivateData(new int(i));

        insert(item);
    }
}

void VCatagoryEffectRow::animate()
{
    int i;
    VCatagoryEffectItem* item = NULL;

    if (_expanding)
    {

        _time = VMain::getClock();

        for (i = 0; i != VCatagoryEffectRow::item_count; ++i)
        {
            item = (VCatagoryEffectItem*)(_children->at(i));
            item->move(_x + (item_x_margin + item_w)
                       * i * (((float)_time - _starttime) / expand_time),
                       _y);
        }

        if (_time - _starttime >= expand_time)
        {
            _expanding = false;
            _starttime = _time = 0;
            return;
        }
    }
}

void VCatagoryEffectRow::expand()
{
    _starttime = VMain::getClock();
    _expanding = true;
}

void VCatagoryEffectRow::move(int x, int y)
{
    VWidget::move(x, y);
}